package com.fh.mq;


import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fh.model.loan.ReducteOverdueInfo;
import com.fh.service.penalty_interest_reduction.IReducteOverdueInfoService;
import com.fh.util.JsonUtil;
import com.ybd.common.component.redis.StringRedisUtil;

/**
 * b status
 * 
 * @author
 * 
 *
 */
@Component
public class ReturnFeeListener implements MessageListener {
	
	private static final Logger log = LoggerFactory.getLogger(ReturnFeeListener.class);
	
	@Autowired
	private IReducteOverdueInfoService service;
	@Autowired
	private StringRedisUtil stringRedisUtil;

	@Override
	public void onMessage(Message arg0) {
		byte[] body = arg0.getBody();
		String res = null ;
		try {
			 res = new String(body, "UTF-8");
			 log.info(String.format("接受罚息退回MQ的信息:%s", res));
			 if (StringUtils.isBlank(res)) {
				 throw new RuntimeException("罚息退回MQ返回的信息为空或者null");
			 }
			 //将获取的数据信息装成map
			 Map map = new HashMap();
			 map = JsonUtil.jsonToMap(res);
			 //修改对应的罚息减免状态
			 ReducteOverdueInfo info = new ReducteOverdueInfo();
			 info.setReduApplyId(String.valueOf(map.get("orderNo")));
			 if (String.valueOf(map.get("code")).equals("200")) {
				info.setReturnStatus(1);//成功
				//如果成功了就去修改主表的罚息退回金额
				service.updateReturnFeeMoney(info.getReduApplyId());
			 }else{
				info.setReturnStatus(2);//失败
			 }
			 service.updateReturnFeeStatus(info.getReduApplyId(), info.getReturnStatus());
		} catch (Exception e) {
			log.error("接受罚息退回信息报错", e);
		}
	}

	
}